#include <stdio.h>
#include <unistd.h>
#include <iot_gpio.h>
#include <iot_gpio_ex.h>
#include <iot_spi.h>
#include "oled.h"
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "gui.h"
#include "temp_humi_demo.h"

#define OLED_TASK_STACK_SIZE 4096

// width: 40, height: 28
const unsigned char col[] = { 0x7f,0xe0,0x00,0x01,0xff,0x7f,0xf0,0x00,0x07,0xff,0x3f,0xf8,0x00,0x0f,0xe0,0x18,0x7c,0x00,0x1f,0x08,0x1c,0x3e,0x00,0x3e,0x18,0x1e,0x0f,0x80,0xf8,0x38,0x1f,0x07,0xc1,0xf0,0x78,0x1f,0x03,0xe3,0xe0,0x78,0x1f,0x05,0xe7,0xc0,0x78,0x1f,0x03,0xcf,0x80,0x7c,0x1f,0x01,0xef,0x00,0x7c,0x1f,0x10,0xee,0x0c,0x7c,0x1f,0x1c,0x7e,0x1c,0x7c,0x1f,0x1e,0x3c,0x38,0x7c,0x1d,0x1e,0x1c,0x7c,0x7c,0x19,0x1f,0x08,0x7c,0x6c,0x19,0x1f,0x00,0x7c,0x6c,0x19,0x1f,0x00,0x7c,0x6e,0x19,0x1f,0x00,0x7c,0x6c,0x0d,0x1f,0x00,0x7c,0x58,0x03,0x1d,0x00,0x7c,0x70,0x00,0x1d,0x00,0x7c,0x40,0x00,0x1d,0x00,0x7c,0x00,0x00,0x1d,0x00,0x5c,0x00,0x00,0x0d,0x00,0x50,0x00,0x00,0x03,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };

 /* task处理函数 */
void OLedTask(void * para)
{
    temp_humi_init();
    printf("OLED task \r\n");
    OLED_Init();               //初始化OLED  
    while(1)
    {
        float temp=get_temp(),humi=get_humi();
        char str_temp[20],str_humi[20];
        sprintf(str_temp,":%.2f",temp);
        sprintf(str_humi,":%.2f",humi);
        OLED_Clear(0);    
        GUI_ShowChinese(0,0,16,"温度",1);
        GUI_ShowString(32,0,str_temp,16,1);
        GUI_ShowChinese(0,20,16,"湿度",1);
        GUI_ShowString(32,20,str_humi,16,1);
        OLED_Display();
        sleep(1);
    }
}
 /* 创建oled任务函数 */
void oled_demo(void)
{    
    int ret;
    oled_gpio_io_init();
    screen_spi_master_init(0); 
    osThreadAttr_t attr;
 
    attr.name = "OLedTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = OLED_TASK_STACK_SIZE;
    attr.priority = 20;
    if (osThreadNew((osThreadFunc_t)OLedTask, NULL, &attr) == NULL) {
        printf("[OLedExample] Falied to create LedTask1!\n");
    }
}
APP_FEATURE_INIT(oled_demo);